package com.ymttest.database.sqlwapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ymt.core.tool.Logger;
import com.ymt.database.SqlManager;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;

/************************************************************************************
 * @File name :DefaultWapper.java
 * @Author : zhouyi
 * @Date : 2015年6月1日
 * @Copyright : 洋码头
 ************************************************************************************/
public class LoginHistoryWapper {
	SqlManager manage;

	public LoginHistoryWapper() {
		manage = new SqlManager("xlobo" + EnvSetup.getEnv());
	}

	/**
	 * id查询UserRegHistory
	 * 
	 * @param m
	 *            m.put("Uid", 3795293);
	 * @return {RegSource=MatouApp, _id=bc3f5d8170ea417f9b534f4e388bd5bf,
	 *         LoginId=testcpw8m2, CreateTime=2015-09-08 14:29:13.057,
	 *         TerminalSource=Other, Mobile=13402198436, ClientIp=172.16.2.191,
	 *         UserId=3795293, ChannelSource=Other}
	 * @throws InterruptedException 
	 */
	public List<Map> selectUserRegHistoryByUid(Map m) throws InterruptedException {
		
		List<Map> pia = manage.getSelectResults(
				"com.ymttest.database.mapping.LoginHistoryMapper.selectUserRegHistoryByUid", m);
		manage.commit();
		if(pia.size()==0){
			Logger.debug("历史注册记录表没搜索到注册记录,等待3s后再次查询");
			Thread.sleep(3000);
			pia = manage.getSelectResults(
					"com.ymttest.database.mapping.LoginHistoryMapper.selectUserRegHistoryByUid", m);
			manage.commit();
		}
		return pia;
	}
	public List<Map> selectLastTokenHistoryByTokenId(String token) throws InterruptedException {
		Map m=new HashMap<>();
		m.put("tokenid", token);
		List<Map> pia = manage.getSelectResults(
				"com.ymttest.database.mapping.LoginHistoryMapper.selectLastTokenHistoryByTokenId", m);
		manage.commit();
		if(pia.size()==0){
			Logger.debug("Token日志记录表没搜索到记录,等待3s后再次查询");
			Thread.sleep(3000);
			pia = manage.getSelectResults(
					"com.ymttest.database.mapping.LoginHistoryMapper.selectLastTokenHistoryByTokenId", m);
			manage.commit();
		}
		return pia;
	}
	/**
	 * uid查询Token登入历史信息
	 * 
	 * @param m
	 *            m.put("Uid", 3795293);
	 * @return {dAddTime=2015-09-09 10:26:25.083, sSummary=ok,
	 *         _id=88486b6355684c52869e6ac671d878b8, iAction=3795328,
	 *         RunTime=2.0477, sDesc=gettoken, sLoginId=6551
	 *         A0611B0F5A2E5CBBC18465B1BF3534D25874E7CD05E8EFECA248D434EEACDA2F2C1FD4D7D453B8B7A13196177E326FE8133E5AA72346
	 *         , sClientType=MainSite}
	 */
	public List<Map> selectTokenHistoryByUid(Map m) {
		List<Map> pia = manage.getSelectResults(
				"com.ymttest.database.mapping.LoginHistoryMapper.selectTokenHistoryByUid", m);
		return pia;
	}

	public int selectTokenHistoryCountByUid(Map m) {
		int count = (Integer) manage.getSelectResults(
				"com.ymttest.database.mapping.LoginHistoryMapper.selectTokenHistoryCountByUid", m).get(0);
		return count;
	}

	public List<Map> selectLoginHistory(String sLoginId, int iAction, String sLoginIp, String sClientType){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sLoginId", sLoginId);
		params.put("iAction", iAction);
		params.put("sLoginIp", sLoginIp);
		params.put("sClientType", sClientType);
		params.put("addTime", YMTDateUtil.getBeforeOrNextSecond(-5));
		List<Map> loginHistory = manage.getSelectResults(
				"com.ymttest.database.mapping.LoginHistoryMapper.selectLoginHistory", params);
		return loginHistory;
	}
	/**
	 * 查询最后20条注册记录
	 * @return
	 */
	public List<Map> selectLastUserRegHistory(){
		List<Map> loginHistory = manage.getSelectResults(
				"com.ymttest.database.mapping.LoginHistoryMapper.selectLastUserRegHistory", null);
		return loginHistory;
	}
	/**
	 * 关闭数据库连接
	 */
	public void close() {
		manage.close();
	}

	@Override
	protected void finalize() throws Throwable {
		this.close();
		manage = null;
		super.finalize();
	}

	public static void main(String args[]) {
		HashMap m = new HashMap();
		m.put("Uid", 3383);
		
		Logger.debug(new LoginHistoryWapper().selectTokenHistoryCountByUid(m));

	}
}
